<template>
  <div :class="['message', { 'user-message': message.isUser }]">
    <div :class="['content', { 'admin-content': !message.isUser}]">
      <div class="demo-collapse" v-if="message.isReaction"  style="font-size: 12px;color: #cccccc;width: 90%;padding-left: 15px">
        <el-collapse v-model="activeModel" >
          <el-collapse-item title="思考过程：" name="1">
            <div v-html="renderedMarkdownReaction" style="border-left: 1px solid  #cccccc !important;padding-left: 5px"></div>
          </el-collapse-item>
        </el-collapse>
      </div>
      <div v-html="renderedMarkdown" class="text markdown-body"></div>
      <div class="time">
        <div>{{ message.time }}</div>
        <div v-if="!message.isTips">
          <el-link
              type="primary"
              :underline="false"
              title="点击复制"
              @click="copyMessageText(message.text)"
              class="optionBtn"
          >
            <el-icon>
              <CopyDocument/>
            </el-icon>
          </el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import "highlight.js/styles/github.css";
import {CopyDocument} from "@element-plus/icons-vue";
import md from '@/api/tool/markdownRenderer.js';

const {proxy} = getCurrentInstance();
const activeModel = ref('1')


const t = defineProps({
  message: {
    type: Object,
    required: true
  }
})


onMounted(() => {
  // 编译 Markdown
  // renderedMarkdown.value = md(t.message.text || '');
  // renderedMarkdownReaction.value = md(t.message.reactionText || '');
  // 添加复制功能
  setupCopyCode();
});

const renderedMarkdownReaction = computed(() =>
    md(t.message.reactionText || '')
);

const renderedMarkdown = computed(() =>
    md(t.message.text || '')
);



function setupCopyCode() {
  // 等待 DOM 更新
  setTimeout(() => {
    // 复制按钮监听事件
    const copyButtons = document.querySelectorAll('.copy-button');
    copyButtons.forEach(button => {
      button.onclick = function () {
        const code = decodeURIComponent(button.getAttribute('data-code'));
        copyMessageText(code);
      }
    });
  }, 0);
}


function copyMessageText(text) {
  // 复制answer
  if (text !== null) {
    navigator.clipboard.writeText(text).then(() => {
      proxy.$modal.msgSuccess("复制成功");
    }).catch(() => {
      proxy.$modal.msgError("复制失败");
    });
  } else {
    proxy.$modal.msgError("内容为空，复制失败");
  }
}


</script>

<style scoped>

.el-collapse {
  border-top: none !important;
}

/* 如果需要，去除子组件的边框 */
.demo-collapse :deep(.el-collapse-item__header),
.demo-collapse :deep(.el-collapse-item__wrap) {
  border-bottom: none !important;
}

/* 全局Markdown样式 */
:deep() pre {
  background: #f6f8fa;
  border-radius: 4px;
}

:deep() code {
  font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace !important;;
  background: none !important;
  line-height: 22px !important;
  font-size: 14px;
  word-break: break-word;
}

.markdown-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

/* 基础样式 */
.markdown-body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.6;
  word-wrap: break-word;
}

.markdown-body :deep() h1, .markdown-body :deep() h2, .markdown-body :deep() h3,
.markdown-body :deep() h4, .markdown-body :deep() h5, .markdown-body :deep() h6 {
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: 600;
  line-height: 1.25;
}

.markdown-body :deep() pre {
  background-color: #f6f8fa;
  border-radius: 6px;
  padding: 16px;
  overflow: auto;
  margin: 16px 0;
}

.markdown-body :deep() code {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
  font-size: 85%;
  padding: 0.2em 0.4em;
  margin: 0;
  background-color: rgba(27, 31, 35, 0.05);
  border-radius: 3px;
}

.markdown-body :deep() pre code {
  background-color: transparent;
  padding: 0;
  margin: 0;
  border-radius: 0;
  max-height: 280px;
  overflow: auto;
}

/* 代码块样式 */
.code-block {
  position: relative;
  border-radius: 6px;
  overflow: hidden;
  margin: 16px 0;
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background-color: #f6f8fa;
  border-bottom: 1px solid #eaecef;
  font-size: 12px;
  font-weight: 500;
  color: #586069;
}

.code-language {
  background-color: rgba(27, 31, 35, 0.05);
  padding: 2px 6px;
  border-radius: 3px;
}

.copy-button:hover {
  color: #0366d6;
}

.copy-button svg {
  width: 14px;
  height: 14px;
}

/* 复制成功状态 */
.copy-button.copied {
  color: #22863a;
}

.copy-button.copied svg {
  display: none;
}

.copy-button.copied::after {
  content: "已复制!";
  font-size: 12px;
}

.message {
  display: flex;
  margin-bottom: 5px;
}

.user-message {
  flex-direction: row-reverse;
}

.avatar {
  margin: 5px 10px;
}

.content {
  max-width: 100%;
  padding: 0 15%;
}

.admin-content {
  width: 100%;
}

.text {
  padding: 0.5px 10px;
  border-radius: 4px;
  word-wrap: break-word;
}

.admin-message {
  width: 100%;
}

.user-message .text {
  background-color: #f3f3f3;
  border-radius: 12px 2px 12px 12px;
  font-size: 16px;
  text-align: right;
}

.time {
  margin-top: 5px;
  font-size: 12px;
  color: #999;
  text-align: left;
}

.user-message .time {
  text-align: right;
  margin-bottom: 35px;
}

.optionBtn {
  margin-right: 5px;
}
</style>